package com.gylang.mail.service.impl;

import com.gylang.common.model.mail.MailBean;
import com.gylang.common.model.mail.TemplateMailBean;
import com.gylang.mail.config.MailProperties;
import com.gylang.mail.service.SendMailService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import javax.mail.internet.MimeMessage;
import java.util.HashMap;
import java.util.Map;

/**
 * @author gylang,
 * @data 2019/11/25 20:29,
 * @DESC
 */
@Slf4j
@Service
public class SendMailServiceImpl implements SendMailService {

    @Autowired
    private JavaMailSender javaMailSender;
    @Autowired
    private MailProperties mailProperties;
    @Autowired
    private Configuration configuration;

    @Override
    public boolean simpleMail(MailBean mailBean) {

        try {
            SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
            //邮件发送人
            simpleMailMessage.setFrom(mailProperties.getUsername());
            //邮件接收人
            simpleMailMessage.setTo(mailBean.getRecevice());
            //邮件主题
            simpleMailMessage.setSubject(mailBean.getSubject());
            //邮件内容
            simpleMailMessage.setText(mailBean.getContent());
            javaMailSender.send(simpleMailMessage);
            return true;
        } catch (Exception e) {
            log.error("邮件发送失败 {}", e.getMessage());
            return false;
        }
    }

    @Override
    public boolean htmlMail(MailBean mailBean) {

        MimeMessage mimeMailMessage = null;
        try {
            mimeMailMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMailMessage, true);
            mimeMessageHelper.setFrom(mailProperties.getUsername());
            mimeMessageHelper.setTo(mailBean.getRecevice());
            mimeMessageHelper.setSubject(mailBean.getSubject());
            StringBuilder sb = new StringBuilder();
            sb.append("<html lang=\"en\">\n" +
                    "<head>\n" +
                    "    <meta charset=\"UTF-8\">\n" +
                    "    <title>Title</title>\n" +
                    "</head>\n" +
                    "\n" +
                    "<body>\n" +
                    " <iframe width=\"100%\" height=\"100%\" src=\"");
            sb.append(mailBean.getContent());
            sb.append(" \"></iframe>" +
                    "</body>\n" +
                    "</html>\n");
            mimeMessageHelper.setText(sb.toString(), true);
            javaMailSender.send(mimeMailMessage);
            return true;
        } catch (Exception e) {
            log.error(e.getMessage());
            return false;
        }
    }

    @Override
    public boolean templateMail(TemplateMailBean templateMailBean) {

        MimeMessage mimeMessage = null;
        try {
            //创建发送对象
            mimeMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
            mimeMessageHelper.setFrom(mailProperties.getUsername());
            mimeMessageHelper.setTo(templateMailBean.getReceiver());
            mimeMessageHelper.setSubject(templateMailBean.getSubject());
            //构建邮箱内容模板
            Map<String, Object> model = new HashMap<String, Object>();
            model.putAll(templateMailBean.getContent());

//            model.put("title", "标题Mail中使用了FreeMarker");
            Template template = configuration.getTemplate("resetPassWd.ftl");
            String text = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
            mimeMessageHelper.setText(text, true);
            javaMailSender.send(mimeMessage);
            return true;
        } catch (Exception e) {
            log.error(e.getMessage());
            return false;
        }

    }
}
